<template>
  <a-modal
    title="新增人员"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="员工姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['staffName', {rules: [{required: true, message: '请输入员工姓名'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="人员性别"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-radio-group v-decorator="['staffSex', {initialValue: '男', rules: [{required: true, message: '请选择性别', trigger: 'blur'}]}]" >
                <a-radio :value="'男'">男</a-radio>
                <a-radio :value="'女'">女</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="所在部门"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-tree-select
                v-decorator="['deptId', {rules: [{required: true, message: '请选择部门'}]}]"
                style="width: 100%"
                :tree-data="deptsTree"
                :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                placeholder="请选择"
                allow-clear
                :treeDefaultExpandedKeys="expandedKeys"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="电话号码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['staffTel', {rules: [{required: true, message: '请输入电话号码'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="出生日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-date-picker
                :format="'YYYY-MM-DD'"
                :fullscreen="false"
                @panelChange="onPanelChange"
                v-decorator="['staffBirth', {rules: [{required: true, message: '请选择'}]}]"
                style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="备注信息"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['staffInfo', {rules: [{ message: '请输入备注信息'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import { addStaff } from '@/api/basic/staff'
import { TreeSelect } from 'ant-design-vue'

export default {
  components: {
    ATreeSelect: TreeSelect
  },
  props: {
    deptsTree: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      expandedKeys: [1],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      data: [],
      value: undefined,
      meteData: [],
      visible: false,
      sex: [
        {
          staffSex: '男'
        },
        {
          staffSex: '女'
        }],
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    add () {
      this.visible = true
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          addStaff(values)
            .then(res => {
              if (res.data.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.confirmLoading = false
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    },
    onPanelChange (value, mode) {
      console.log(value, mode)
    }
  }
}
</script>
